package com.woyou.ui.component;

import com.citaq.ideliver.R;
import com.woyou.ui.activity.LocationLookBookActivity_;
import com.woyou.ui.api.IStrategy;
import com.woyou.utils.Utils;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * @author longtao.li
 * 
 */
public class ErrorHintView extends RelativeLayout {

	RelativeLayout mContainer;
	LayoutParams layoutParams;
	/**
	 * 动画
	 */
	private AnimationDrawable animationDrawable;
	
	private ErrorHandler mErrorHandler = new ErrorHandler();
	
	public interface OperateListener{
		void operate();
	}
	
	private OperateListener mOperateListener;
	
	public ErrorHintView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ErrorHintView(Context context) {
		super(context);
		init();
	}

	private void init() {
		View.inflate(getContext(), R.layout.custom_error_hint_view, this);
		mContainer = (RelativeLayout) findViewById(R.id.container);
		layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	}
	
	public void show(){
		setVisibility(View.VISIBLE);
	}
	
	public void close(){
		setVisibility(View.GONE);
	}
	
	/**
	 * 锦囊 用于实施策略,处理订单状态
	 * 
	 * @author longtao.li
	 * 
	 */
	class ErrorHandler{

		public ErrorHandler() {
		}

		public void operate(IStrategy strategy) {
			show();
			strategy.operate();
		}

	}
	
	
	/**
	 * 显示加载失败UI
	 */
	public void loadFailure(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new LoadFailure());
	}
	
	View loadFailure;
	
	/**
	 * 加载失败
	 * @author longtao.li
	 *
	 */
	class LoadFailure implements IStrategy{

		@Override
		public void operate() {
			if( loadFailure == null ){
				loadFailure = View.inflate(getContext(), R.layout.layout_load_failure, null);
				View view = loadFailure.findViewById(R.id.load_retry);
				view.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						mOperateListener.operate();
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(loadFailure,layoutParams);
		}
		
	}
	
	/**
	 * 显示无此订单详情
	 */
	public void loadNoDetail(){
		mErrorHandler.operate(new LoadNoDetail());
	}
	
	View loadNoDetail;
	
	/**
	 * 
	 * @author longtao.li
	 *
	 */
	class LoadNoDetail implements IStrategy{

		@Override
		public void operate() {
			if( loadNoDetail == null ){
				loadNoDetail = View.inflate(getContext(), R.layout.layout_load_noorder_detail, null);
			}
			mContainer.removeAllViews();
			mContainer.addView(loadNoDetail,layoutParams);
		}
		
	}
	
	/**
	 * 显示无评论
	 */
	public void noComments(String msg){
		mErrorHandler.operate(new NoComments(msg));
	}
	
	View noComments;
	
	/**
	 * 无评论
	 * @author longtao.li
	 *
	 */
	class NoComments implements IStrategy{
		
		String msgtext;
		public NoComments(String msgtext) {
			super();
			this.msgtext = msgtext;
		}
		
		@Override
		public void operate() {
			if( noComments == null ){
				noComments = View.inflate(getContext(), R.layout.layout_load_nocomments, null);
				TextView noComment = (TextView) noComments.findViewById(R.id.comments_msg);
				noComment.setText(msgtext);
			}
			mContainer.removeAllViews();
			mContainer.addView(noComments,layoutParams);
		}
		
	}
	
	/**
	 * 显示无数据
	 */
	public void noData(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new NoData());
	}
	
	View noData;
	
	/**
	 * 暂无数据
	 * @author longtao.li
	 *
	 */
	class NoData implements IStrategy{

		@Override
		public void operate() {
			if( noData == null ){
				noData = View.inflate(getContext(), R.layout.layout_load_nodata, null);
			}
			mContainer.removeAllViews();
			mContainer.addView(noData,layoutParams);
			
		}
		
	}
	
	/**
	 * 显示无订单
	 */
	public void noOrder(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new NoOrder());
	}
	
	View noOrder;
	
	/**
	 * 无订单
	 * @author longtao.li
	 *
	 */
	class NoOrder implements IStrategy{

		@Override
		public void operate() {
			if( noOrder == null ){
				noOrder = View.inflate(getContext(), R.layout.layout_load_noorder, null);
				View view = noOrder.findViewById(R.id.load_browse);
				view.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						mOperateListener.operate();
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(noOrder,layoutParams);
			
		}
		
	}
	
	/**
	 * 显示无店铺
	 */
	public void noShops(String msgtext){
		mErrorHandler.operate(new NoShops(msgtext));
	}
	
	View noShops;
	
	/**
	 * 无店铺
	 * @author longtao.li
	 *
	 */
	class NoShops implements IStrategy{

		String msgtext;
		public NoShops(String msgtext) {
			super();
			this.msgtext = msgtext;
		}
		
		@Override
		public void operate() {
			if( noShops == null ){
				noShops = View.inflate(getContext(), R.layout.layout_load_noshops, null);
				TextView isshop = (TextView) noShops.findViewById(R.id.isshop);
				isshop.setText(msgtext);
			}
			mContainer.removeAllViews();
			mContainer.addView(noShops,layoutParams);
			
		}
		
	}
	
	/**
	 * 显示过滤无店铺
	 */
	public void noFilterShops(FilterShopImp mListener, String btntext){
		mErrorHandler.operate(new noFilterShops(mListener, btntext));
	}
	
	View noFilterShops;
	
	/**
	 * 无过滤店铺
	 * @author longtao.li
	 *
	 */
	class noFilterShops implements IStrategy{
		
		private FilterShopImp mListener;
		private String btntext;
		public noFilterShops(FilterShopImp mListener, String btntext) {
			super();
			this.mListener = mListener;
			this.btntext = btntext;
		}

		@Override
		public void operate() {
			if( noFilterShops == null ){
				noFilterShops = View.inflate(getContext(), R.layout.layout_load_filter_noshops, null);
				TextView recommendTv = (TextView) noFilterShops.findViewById(R.id.recommend_tv);
				recommendTv.setText(btntext);
				recommendTv.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mListener.resetShop();
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(noFilterShops,layoutParams);
			
		}
		
	}
	
	/**
	 * 过滤无店铺重刷店铺接口
	 * @author zhou.ni
	 *
	 */
	public interface FilterShopImp{
		void resetShop();
	}
	
	/**
	 * 显示超时UI
	 */
	public void timeOut(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new TimeOut());
	}
	
	View timeOut;
	
	/**
	 * 请求超时
	 * @author longtao.li
	 *
	 */
	class TimeOut implements IStrategy{

		@Override
		public void operate() {
			if( timeOut == null ){
				timeOut = View.inflate(getContext(), R.layout.layout_load_outtime, null);
				View view = timeOut.findViewById(R.id.outtime_retry);
				view.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						mOperateListener.operate(); 
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(timeOut,layoutParams);
		}
		
	}
	
	/**
	 * 显示无网络
	 */
	public void netError(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new NetWorkError());
	}
	
	View netError;
	
	/**
	 * 无网络
	 * @author longtao.li
	 *
	 */
	class NetWorkError implements IStrategy{

		@Override
		public void operate() {
			if( netError == null ){
				netError = View.inflate(getContext(), R.layout.layout_load_wifi_failure, null);
				View view = netError.findViewById(R.id.wifi_retry);
				view.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						mOperateListener.operate();
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(netError,layoutParams);
		}
		
	}
	
	/**
	 * 显示定位失败
	 */
	public void locationFail(OperateListener Listener){
		this.mOperateListener = Listener;
		mErrorHandler.operate(new LocationFail());
	}
	
	View locationFail;
	
	/**
	 * 定位失败
	 * @author longtao.li
	 *
	 */
	class LocationFail implements IStrategy{

		@Override
		public void operate() {
			if( locationFail == null ){
				locationFail = View.inflate(getContext(), R.layout.layout_location_failure, null);
				View view = locationFail.findViewById(R.id.location_retry);
				View location_lookbook = locationFail.findViewById(R.id.location_lookbook);
				view.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						mOperateListener.operate();
					}
				});
				location_lookbook.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						clazz = LocationLookBookActivity_.class;
						Utils.openActivity(getContext(), clazz, null);
						
					}
				});
			}
			mContainer.removeAllViews();
			mContainer.addView(locationFail,layoutParams);
		}
		
	}
	
	View loadingdata;
	
	@SuppressWarnings("rawtypes")
	Class clazz;
	
	/**
	 * 转菊花
	 * @author 荣
	 */
	class LoadingData implements IStrategy{

		@Override
		public void operate() {
			if( loadingdata == null ){
				loadingdata = View.inflate(getContext(), R.layout.layout_load_loading, null);
			}
			ImageView iv = (ImageView)loadingdata.findViewById(R.id.loading_iv);
			mContainer.removeAllViews();
			mContainer.addView(loadingdata,layoutParams);
			showLoading(iv);
		}
		
	}

	/**
	 * 转菊花
	 * @param Listener
	 */
	public void loadingData(){
		mErrorHandler.operate(new LoadingData());
	}

	/**
	 * 显示动画loading
	 */
	public void showLoading(final ImageView iv) {
		animationDrawable = (AnimationDrawable) iv.getBackground();
		animationDrawable.start();
	}

	/**
	 * 隐藏动画loading
	 */
	public void hideLoading() {
		if (animationDrawable != null && animationDrawable.isRunning()) {
			animationDrawable.stop();
		}
		close();
	}

}
